const user = require("../model/user")
const crypto = require("crypto");//加密模块

module.exports = function (req, res) {

    var psw = crypto.createHash('sha256').update(req.query.oldpsw).digest("hex");
    user.findOne({ _id: req.query._id }).then(result => {
        if (result.password === psw) {
            var newpsw = crypto.createHash('sha256').update(req.query.newpsw).digest("hex");
            user.updateOne({ _id: req.query._id }, { $set: { password: newpsw } }).then(result => {
                if (result) {
                    res.send({
                        code: 0,
                        message: "密码修改成功，请重新登陆"
                    })
                    req.session.destroy();
                } else {
                    res.send({
                        code: 1,
                        message: "服务器出错，密码修改失败"
                    })
                }
            })
        } else {
            res.send({
                code: 2,
                message: "该账户的原密码错误，请重新输入"
            })
        }
    })
}